package com.Sep_Thirtyy;

import java.util.Arrays;

/**
 * 面试题 01.08. 零矩阵
 * 编写一种算法，若M × N矩阵中某个元素为0，则将其所在的行与列清零。
 * https://leetcode.cn/problems/zero-matrix-lcci/
 *
 * 2022/09/30 14:03
 */

public class Solution01_08 {

     static int[][] matrix;

    public void setZeroes(int[][] matrix) {
        int[] row=new int[matrix.length];
        int[] col=new int[matrix[0].length];
        for (int i=0;i<matrix.length;i++)
        {
            for (int j=0;j<matrix[0].length;j++)
            {
                if (matrix[i][j]==0) row[i]=col[j]=1;
            }
        }
        for (int i=0;i<matrix.length;i++)
        {
            for (int j=0;j<matrix[0].length;j++)
            {
                if (row[i]==1||col[j]==1) matrix[i][j]=0;
            }
        }
    }

    public static void main(String[] args) {
        matrix=new int[][]{
                {1,1,1},
                {1,0,1},
                {1,1,1}
        };
        Solution01_08 solution01_08=new Solution01_08();solution01_08.setZeroes(matrix);
        System.out.println(Arrays.deepToString(matrix));
    }
}
